<template>
	<view>
		<scroll-view scroll-y class="DrawerPage" :class="modalName=='viewModal'?'show':''">
			<cu-custom class="text-gray" bgColor="bg-gradual" :isBack="true">
				<block slot="backText">返回</block>
				
				<block  slot="content">
					<view  >
						<text  style="font-size:50rpx"  class="cuIcon-friendfavor"></text> 
					</view>
					
				</block>

			</cu-custom>
			<!-- <tabbar></tabbar> -->
			<view @tap="showModal" data-target="viewModal" class="blockposition">
				
			</view>
			<view class="cu-chat" id="chatRecord">
				

				<view  class="cu-item" :class="item._self ? 'self':''" v-for="(item,i) in chatList" :key='i'>
					<view v-if="!item._self" class="cu-avatar radius" style="background-image:url(../../../static/images/man.jpg);"></view>
					
					
					<view class="main">
						<view class="content shadow">
							{{item.content}}
						</view>
					</view>
					
					<view v-if="item._self" class="cu-avatar radius" style="background-image:url(../../../static/images/nv.jpg);"></view>
					
					<view class="date">{{item.time}}</view>
				</view>
				
				
				
			</view>

			<view class="cu-bar foot input" :style="[{bottom:InputBottom+'px'}]">
			<view class="action">
				<text class="cuIcon-sound text-grey"></text>
			</view>
			<input class="solid-bottom" :adjust-position="false" :focus="false" maxlength="300" cursor-spacing="10"
			 @focus="InputFocus" @blur="InputBlur" v-model="value"></input>
			<view class="action">
				<text class="cuIcon-emojifill text-grey"></text>
			</view>
			<button @click="sendMsg" class="cu-btn bg-green shadow">发送</button>
		</view>
		
		</scroll-view>
		
		
		<!-- 全屏抽屉 -->
		<view class="DrawerClose" :class="modalName=='viewModal'?'show':''" @tap="hideModal">
			<text class="cuIcon-pullright"></text>
		</view>
		<scroll-view scroll-y class="DrawerWindow" :class="modalName=='viewModal'?'show':''">
			<view class="cu-list menu card-menu margin-top-xl margin-bottom-xl shadow-lg">
				<view class="cu-item arrow" @click="toOperation(item)" v-for="(item,index) in operationList" :key="index">
					<view class="content">
						<text class="cuIcon-github text-grey"></text>
						<text class="text-grey">{{item.text}}</text>
					</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
		import tabbar from '@/component/tabbar/tabbar.vue'
	export default {
		components:{
			tabbar
		},
		data() {
			return {
				InputBottom: 0,
				
				db:null,
				value:'',
				chatList:[
					{
						content:'喵喵喵！喵喵！喵！点击上方图标，可以查看我有哪些功能哦',
						time:'2018年3月23日 13:23',
						_self:true
					},
					{
						content:'喵喵喵！喵喵喵！',
						time:'13:23',
						_self:false
					}
				],
				operationList:[
					{text:'数学计算 ',simple:'138+59-60*2/3'},
					{text:'成语典故 ',simple:'马到成功'},
					{text:'英汉词库 ',simple:'love'},
					{text:'天气查询 ',simple:'杭州天气'},
					{text:'垃圾分类 ',simple:'香蕉皮是什么垃圾'},
					{text:'故事大全 ',simple:'讲个故事'},
					{text:'成语接龙 ',simple:'成语接龙'},
					{text:'周公解梦 ',simple:'梦见你'},
					{text:'新闻资讯 ',simple:'男子未在指定医院购药保险公司拒赔 法院判了'},
					{text:'绕口令 ',simple:'绕口令'},
					{text:'歇后语 ',simple:'歇后语'},
					{text:'笑话大全 ',simple:'笑话、讲个笑话'},
					{text:'唐诗宋词 ',simple:'唐诗、宋词'},
					{text:'土味情话 ',simple:'情话、讲个情话'},
					{text:'旅游景点',simple:'杭州'},
					{text:'顺口溜',simple:'顺口溜'},
					{text:'名人名言',simple:'名人名言、名言'},
					{text:'毒鸡汤',simple:'毒鸡汤'},
					{text:'生活小窍门',simple:'窍门、生活小窍门'},
					{text:'十万个为什么',simple:'为什么胃液是酸的？'},
					{text:'中药大全',simple:'艾纳香'},
					{text:'西药查询',simple:'舒萨林'},
					{text:'姓氏大全',simple:'邹'},
					{text:'明星查询',simple:'彭于晏'},
					{text:'动物查询',simple:'猫咪'},
					{text:'舔狗日记',simple:'舔狗'},
					{text:'电影台词',simple:'电影台词'},
					{text:'彩虹屁 ',simple:'彩虹屁'}
				],
				modalName:null
			};
		},
		methods: {
			toOperation(item){
				this.value=item.simple;
				this.hideModal()
				this.sendMsg()
			},

			showModal(e) {
				console.log(e);
				this.modalName = e.currentTarget.dataset.target
			},
			hideModal(e) {
				this.modalName = null
			},
			tabSelect(e) {
				this.TabCur = e.currentTarget.dataset.id;
				this.scrollLeft = (e.currentTarget.dataset.id - 1) * 60
			},
			
			
			async sendMsg(){

				let obj={
					content:this.value,
					time:this.getDate(),
					_self:true
				}
				this.chatList.push(obj)
				

				const data=await uni.request({
					url:'http://api.tianapi.com/txapi/robot/index',
					method:'GET',
					data:{
						key:'1a162a6b0fec66a258dc8f80a8c9cfdb',
						question:this.value,
						uniqueid:'1a162a6b0fec66a258dc8f80a8c9cfdb',
						mode:0,
					}
				})
				this.value=''
				
				let list=data[1].data.newslist
	
				
				 obj={
					content:list[0].reply,
					time:this.getDate(),
					_self:false
				}
				this.chatList.push(obj)
				
				this.getWindowHeight()
			},
			
			getDate(num=0){
					//num=0 代表今天，1表示前一天，2表示后一天
					var time=(new Date).getTime()-24*60*60*1000*num;
					var yesterday=new Date(time);
					var month=yesterday.getMonth();
					var day=yesterday.getDate();
					var hour=yesterday.getHours();
					var miunte=yesterday.getMinutes();
					// yesterday=yesterday.getFullYear() + "年" + (yesterday.getMonth()> 9 ? (yesterday.getMonth() + 1) : "0" + (yesterday.getMonth() + 1)) + "月" +(yesterday.getDate()> 9 ? (yesterday.getDate()) : "0" + (yesterday.getDate()))+ "日 " +hour+":" +miunte;
					
					yesterday=hour+":" +miunte;
					 return yesterday
			
			},
			
			

			
			
			InputFocus(e) {
				this.InputBottom = e.detail.height
				this.value=e.detail.value
			},
			InputBlur(e) {
				this.InputBottom = 0;
				this.value=e.detail.value
			},
			
			
			getWindowHeight(){
				
				uni.createSelectorQuery().select("#chatRecord").boundingClientRect((res)=>{
	
							uni.pageScrollTo({
								selector:'#chatRecord',
							    scrollTop: res.bottom,
							    duration: 300
							});
							
						}).exec();
	

			}
		},
		onLoad() {
			console.log(window.documnet);
		}
	}
</script>

<style>
page{
  padding-bottom: 100upx;
}

.blockposition{
	width: 80rpx;
	height: 80rpx;
	/* background-color: red; */
	position: absolute;
	z-index: 20000;
	top: 60rpx;
	left: 50%;
	transform: translate(-50%);
}


page {
		/* background-image: var(--gradualBlue); */
		width: 100vw;
		overflow: hidden;
	}

	.DrawerPage {
		position: fixed;
		width: 100vw;
		height: 100vh;
		left: 0vw;
		padding-bottom: 60rpx;
		
		transition: all 0.4s;
	}

	.DrawerPage.show {
		transform: scale(0.9, 0.9);
		left: 85vw;
		box-shadow: 0 0 60upx rgba(0, 0, 0, 0.2);
		transform-origin: 0;
	}

	.DrawerWindow {
		position: absolute;
		width: 85vw;
		height: 100vh;
		left: 0;
		top: 0;
		transform: scale(0.9, 0.9) translateX(-100%);
		opacity: 0;
		pointer-events: none;
		transition: all 0.4s;
		padding: 100upx 0;
	}

	.DrawerWindow.show {
		transform: scale(1, 1) translateX(0%);
		opacity: 1;
		pointer-events: all;
	}

	.DrawerClose {
		position: absolute;
		width: 40vw;
		height: 100vh;
		right: 0;
		top: 0;
		color: transparent;
		padding-bottom: 30upx;
		display: flex;
		align-items: flex-end;
		justify-content: center;
		background-image: linear-gradient(90deg, rgba(0, 0, 0, 0.01), rgba(0, 0, 0, 0.6));
		letter-spacing: 5px;
		font-size: 50upx;
		opacity: 0;
		pointer-events: none;
		transition: all 0.4s;
	}

	.DrawerClose.show {
		opacity: 1;
		pointer-events: all;
		width: 15vw;
		color: #fff;
	}

	.DrawerPage .cu-bar.tabbar .action button.cuIcon {
		width: 64upx;
		height: 64upx;
		line-height: 64upx;
		margin: 0;
		display: inline-block;
	}

	.DrawerPage .cu-bar.tabbar .action .cu-avatar {
		margin: 0;
	}

	.DrawerPage .nav {
		flex: 1;
	}

	.DrawerPage .nav .cu-item.cur {
		border-bottom: 0;
		position: relative;
	}

	.DrawerPage .nav .cu-item.cur::after {
		content: "";
		width: 10upx;
		height: 10upx;
		background-color: currentColor;
		position: absolute;
		bottom: 10upx;
		border-radius: 10upx;
		left: 0;
		right: 0;
		margin: auto;
	}

	.DrawerPage .cu-bar.tabbar .action {
		flex: initial;
	}
</style>
